package org.mango.linkedlist;

import org.mango.entity.DATA;

import java.util.Scanner;


/**
 * 
 * @author 戴礼明
 *日期： 2016-4-22
 *content：链表的相关操作
 */
public class LinkedList {

	   DATA data=new DATA();
	   LinkedList nextNode;
	    
	static LinkedList addEnd(LinkedList head,DATA data){
		   LinkedList node=new LinkedList(),htemp;
		    
			   node.data=data;
              node.nextNode=null;
              if(head==null){
            	  head=node;
            	  return head;
              }
              
              htemp=head;
              while(htemp.nextNode!=null){
            	  htemp=htemp.nextNode;
              }
              htemp.nextNode=node;
              return head;
		   }
		   
	LinkedList addFirst(LinkedList head,DATA data){
		LinkedList node=new LinkedList();
		node.data=data;
		node.nextNode=head;
		return node;
	}
	
	static LinkedList findNode(LinkedList head,String key){
		  LinkedList htemp;
		  htemp=head;
		  htemp=htemp.nextNode;
		  while(htemp!=null){
			  DATA data;
			  data=htemp.data;
			  System.out.println(htemp.data.key);
			   if(htemp.data!=null&data.key.compareTo(key)==0){
				    return htemp;
			   }
			   htemp=htemp.nextNode;
		  }
		  return null;
	}
	
	static LinkedList insert(LinkedList head,DATA data,String findKey){
		LinkedList node=new LinkedList(),nodetemp;
		node.data=data;
		nodetemp=findNode(head,findKey);
		if(nodetemp!=null){
			node.nextNode=nodetemp.nextNode;
			nodetemp.nextNode=node;
		}else{
			System.out.println("未找到正确位置！");
		}
		return head;
	}
	
	
	static LinkedList delete(LinkedList head,String key){
		LinkedList node,htemp;
		node=head;htemp=head;
		htemp=htemp.nextNode;
		while(htemp!=null){
			   if(htemp.data.key.compareTo(key)==0){
				         node.nextNode=htemp.nextNode;
				         System.out.println(node.nextNode.data.key);
				         return node;
			   }else{
				   node=htemp;
				   System.out.println(node.data.key);
				   htemp=htemp.nextNode;
			   }
		}
		return node;
	}
	
	  static void showAll(LinkedList node){
		   LinkedList htemp,node1;
		   DATA data;
		   node1=node;
		  htemp=node1;
		   while(htemp!=null){
			   data=htemp.data;
			   htemp=htemp.nextNode;
			   System.out.println("key:"+data.key+"  name:"+data.name+"  age:"+data.age);
		   }
	   }
	   
	   
	static boolean memeryOut(LinkedList node){
		   if(node==null){
			   System.out.println("内存申请失败！");
			   return false;
		   }
		   return true;
	   }
	
	   public static void main(String[] args) {
		   Scanner sc=new Scanner(System.in);
		   LinkedList head = new LinkedList();
		 do{
			 DATA data=new DATA();
			 System.out.println("输入key，name，age");
			 data.key=sc.next();
			 data.name=sc.next();
			 data.age=sc.nextInt();
			 if(data.age==0){
				 break;
			 }
			 head=addEnd(head, data);
		 }while(true);
		 showAll(head);
		/* System.out.println("输入key，name，age");
		 DATA data=new DATA();
		 data.key=sc.next();
		 data.name=sc.next();
		 data.age=sc.nextInt();
		 System.out.println("输入要插入的key");
		 String key=sc.next();*/
		// head=insert(head, data, key);
		 //showAll(head);
		 System.out.println("输入要删除的key");
		 String key1=sc.next();
		 head=delete(head, key1);
		 showAll(head);
	}
	   
}
